package wwk.com.android_meilishuo.mvp.model;

import android.content.Context;
import android.widget.Toast;

import com.wwk.core.net.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import wwk.com.android_meilishuo.interfaces.MLSInterface;
import wwk.com.android_meilishuo.ui.secondfloor.DataBean;
import wwk.com.android_meilishuo.ui.secondfloor.EmptyLayout;
import wwk.com.android_meilishuo.ui.secondfloor.SecondFloorActivity;
import wwk.com.android_meilishuo.ui.secondfloor.SecondFloorBean;

/**
 * Created by Administrator on 2016/12/28.
 */

public class SecondFloorBiz implements MLSModel.ISecondFloorBiz {
    private MLSInterface mlsInterface;

    /**
     * 网络加载
     *
     * @param onSecondFloorListener
     */
    @Override
    public void loadSecondFloorData(final MLSModel.OnSecondFloorListener onSecondFloorListener, Context context, final EmptyLayout sf_EmptyLayout) {
        Retrofit retrofit_SF = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_SFY)
                //gson解析
                .addConverterFactory(GsonConverterFactory.create())
                //支持rxjava
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        if (retrofit_SF != null) {
            mlsInterface = retrofit_SF.create(MLSInterface.class);
        }
        Map<String, String> map = new HashMap<>();
        map.put("limit", "200");
        Observable observable = mlsInterface.getSFData("android", map);
        sf_EmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        //判断是否有网络链接
        if (EmptyLayout.isConnectivity(context)) {
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<SecondFloorBean>() {
                        @Override
                        public void call(SecondFloorBean secondFloorBean) {
                            if (secondFloorBean != null) {
                                List<DataBean> list = secondFloorBean.getData();
                                onSecondFloorListener.onSuccess(list);
                            }
                        }
                    });

        } else {
            //设置无网络状态
            sf_EmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }
    }
}
